let curPage = 1;
let pageSize = 10;
layui.use(['laypage', 'layer'], function(){
    let laypage = layui.laypage
        ,layer = layui.layer;
    laypage.render({
        elem: 'page'
        ,count: getCount()
        ,layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
        ,jump: function(obj){
        curPage = obj.curr;
        pageSize = obj.limit;
        findAll(curPage ,pageSize)
    }
    });
});
function getCount() {
    let count = 0;
    $.ajax({
        url: '/back/user/count',//地址：访问后台的地址
        data: {},//前端传递给后台的数据
        type: 'get',
        async: false,
        dataType: 'json',//是后端传递到前端的数据格式
        success: function (data) {//后端给前端的一个反馈
            count = data;
        }
    });
    return count;
}
function setList(data) {
    let html = '';
    for (let i = 0; i < data.length; i++) {
        html += '<tr>\n' +
            '                            <td>' + data[i].id + '</td>\n' +
            '                            <td>' + data[i].username + '</td>\n' +
            '                            <td>' + setUndefinedNull(data[i].sex) + '</td>\n' +
            '                            <td>' + setUndefinedNull(data[i].phone) + '</td>\n' +
            '                            <td>' + setUndefinedNull(data[i].birthday) + '</td>\n' +
            '                            <td>' + setUndefinedNull(data[i].hobby) + '</td>\n' +
            '                            <td class="td-status">\n' +
            '                                <span class="layui-btn layui-btn-normal layui-btn-mini">' + (data[i].enable == 1 ? "已启用" : "已停用") + '</span></td>\n' +
            '                            <td class="td-manage">\n' +
            '                                <a onclick="member_stop(this,' + data[i].id + ')" href="javascript:;" title="' + (data[i].enable == 1 ? "启用" : "停用") + '">\n' +
            '                                    <i class="layui-icon">&#xe601;</i>\n' +
            '                                </a>\n' +
            '                                <a title="编辑" onclick="edit(' + data[i].id + ')"\n' +
            '                                   href="javascript:;">\n' +
            '                                    <i class="iconfont">&#xe68b;</i>\n' +
            '                                </a>\n' +
            '                                <a onclick="xadmin.open(\'修改密码\',\'member-password.html\',600,400)" title="修改密码"\n' +
            '                                   href="javascript:;">\n' +
            '                                    <i class="layui-icon">&#xe631;</i>\n' +
            '                                </a>\n' +
            '                                <a title="删除" onclick="member_del(this,' + data[i].id + ')" href="javascript:;">\n' +
            '                                    <i class="layui-icon">&#xe640;</i>\n' +
            '                                </a>\n' +
            '                            </td>\n' +
            '                        </tr>';
    }
    $("#tbody").html(html);

}
function findAll(curPage ,pageSize) {
    // loginName: sessionStorage.getItem("loginName"),
    let data = myAjax('/back/user/get', {startPage: (curPage-1)*pageSize,
        pageSize: pageSize}, 'get');
    setList(data.data);
}
    function setUndefinedNull(data) {
        return data == undefined ? "" : data;
    }
